/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;

#endregion

namespace DotNetNuke.ModuleToolkit.Modules.Skinning
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// Helper class with utilities for the Skinning Engine
    /// </summary>
    /// <history>
    ///     [anurse]  12/22/2005  Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public static class SkinUtil
    {

        #region Public Static Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Locates the specified resource within the current skin folder, and
        /// if it is not found, tries the fallback (default) skin folder
        /// </summary>
        /// <param name="skinsFolder">The folder containing skins</param>
        /// <param name="requestedSkin">The skin to search for the file</param>
        /// <param name="fallbackSkin">The skin to fall back on if the requested skin does not contain the file</param>
        /// <param name="relativePath">The path within the skin to search</param>
        /// <returns>The server path to the resource, or the empty string "" if the resource was not found</returns>
        /// <internal>This method is used internally and while it can be called from user code, it is not designed for that purpose</internal>
        /// <history>
        ///     [anurse]  12/22/2005  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string MapSkinPath(string skinsFolder, string requestedSkin, string fallbackSkin, string relativePath)
        {
            string mapPath = String.Empty;

            if (!String.IsNullOrEmpty(requestedSkin))
            {
                string path = String.Format("{0}/{1}/{2}", skinsFolder, requestedSkin, relativePath);
                string lpath = HttpContext.Current.Server.MapPath(path);

                if (File.Exists(lpath) || Directory.Exists(lpath))
                    mapPath = path;
            }

            if (String.IsNullOrEmpty(mapPath))
            {
                string path = String.Format("{0}/{1}/{2}", skinsFolder, fallbackSkin, relativePath);
                string lpath = HttpContext.Current.Server.MapPath(path);
                if (File.Exists(lpath) || Directory.Exists(lpath))
                    mapPath = path;
            }
            return mapPath;
        }

        #endregion

    }
}
